﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using LuaInterface;

namespace Codeology.Auctioneer
{

    public class AuctioneerScanStats
    {

        private DateTime last_scan;
        private DateTime last_full_scan;
        private DateTime image_updated;

        private string server_key;
        private int matched_count;
        private int same_count;
        private int expired_delete_count;
        private int missed_count;
        private DateTime start_time;
        private int update_count;
        private int old_count;
        private int early_delete_count;
        private DateTime end_time;
        private int filtered_count;
        private bool was_incomplete;
        private int current_count;
        private int new_count;
        private bool was_getall;
        private int scan_count;

        internal AuctioneerScanStats(LuaTable scanStats)
        {
            // Get last scan
            long last_scan_number = long.Parse(scanStats["LastScan"].ToString());

            last_scan = DateUtils.UnixToDateTime(last_scan_number);

            // Get last full scan
            long last_full_scan_number = long.Parse(scanStats["LastFullScan"].ToString());

            // Get image updated
            long image_updated_number = long.Parse(scanStats["ImageUpdated"].ToString());

            // Initialize further fields
            server_key = String.Empty;
            matched_count = -1;
            same_count = -1;
            expired_delete_count = -1;
            missed_count = -1;
            start_time = DateTime.MinValue;
            update_count = -1;
            old_count = -1;
            early_delete_count = -1;
            end_time = DateTime.MinValue;
            filtered_count = -1;
            was_incomplete = false;
            current_count = -1;
            new_count = -1;
            was_getall = false;
            scan_count = -1;

            // Look for further table
            foreach(DictionaryEntry entry in scanStats) {
                int value = -1;

                if (int.TryParse(entry.Key.ToString(),out value)) {
                    if (value == 0) {
                        // Get table
                        LuaTable table = (LuaTable)entry.Value;

                        // Set fields
                        server_key = table["serverKey"].ToString();
                        matched_count = int.Parse(table["matchedCount"].ToString());
                        same_count = int.Parse(table["sameCount"].ToString());
                        expired_delete_count = int.Parse(table["expiredDeleteCount"].ToString());
                        missed_count = int.Parse(table["missedCount"].ToString());
                        start_time = DateUtils.UnixToDateTime(long.Parse(table["startTime"].ToString()));
                        update_count = int.Parse(table["updateCount"].ToString());
                        old_count = int.Parse(table["oldCount"].ToString());
                        early_delete_count = int.Parse(table["earlyDeleteCount"].ToString());
                        end_time = DateUtils.UnixToDateTime(long.Parse(table["endTime"].ToString()));
                        filtered_count = int.Parse(table["filteredCount"].ToString());
                        was_incomplete = bool.Parse(table["wasIncomplete"].ToString());
                        current_count = int.Parse(table["currentCount"].ToString());
                        new_count = int.Parse(table["newCount"].ToString());
                        was_getall = bool.Parse(table["wasGetAll"].ToString());
                        scan_count = int.Parse(table["scanCount"].ToString());

                        // Break
                        break;
                    }
                }
            }
        }

        #region Properties

        public DateTime LastScan
        {
            get {
                return last_scan;
            }
        }

        public DateTime LastFullScan
        {
            get {
                return last_full_scan;
            }
        }

        public DateTime ImageUpdated
        {
            get {
                return image_updated;
            }
        }

        public string ServerKey
        {
            get {
                return server_key;
            }
        }

        public int MatchedCount
        {
            get {
                return matched_count;
            }
        }

        public int SameCount
        {
            get {
                return same_count;
            }
        }

        public int ExpiredDeleteCount
        {
            get {
                return expired_delete_count;
            }
        }

        public int MissedCount
        {
            get {
                return missed_count;
            }
        }

        public DateTime StartTime
        {
            get {
                return start_time;
            }
        }

        public int UpdateCount
        {
            get {
                return update_count;
            }
        }

        public int OldCount
        {
            get {
                return old_count;
            }
        }

        public int EarlyDeleteCount
        {
            get {
                return early_delete_count;
            }
        }

        public DateTime EndTime
        {
            get {
                return end_time;
            }
        }

        public int FilteredCount
        {
            get {
                return filtered_count;
            }
        }

        public bool WasIncomplete
        {
            get {
                return was_incomplete;
            }
        }

        public int CurrentCount
        {
            get {
                return current_count;
            }
        }

        public int NewCount
        {
            get {
                return new_count;
            }
        }

        public bool WasGetAll
        {
            get {
                return was_getall;
            }
        }

        public int ScanCount
        {
            get {
                return scan_count;
            }
        }

        #endregion

    }

}
